//
//  HWCommentViewController.m
//  HappyWeekend
//
//  Created by 赵洁 on 16/1/14.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWCommentViewController.h"
#import "IHKeyboardAvoiding.h"
#import "HWLoginAndRegisterViewController.h"
#import "HWTopicsCommentTableViewCell.h"
#import "HWCommentModel.h"
@interface HWCommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)  UIView* targetView;
@property (nonatomic, strong)UIView *avoidingView;
@property (nonatomic, strong)  UITextField* textField1;
@property (nonatomic, strong)  UIButton *but;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSString *message;
@property (nonatomic,strong)NSString *name;
@end

@implementation HWCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"评论";
    [self addView];
    [self setUpdata];
    [IHKeyboardAvoiding setAvoidingView:self.avoidingView withTarget:_targetView];
    
    

}
- (void)setUpdata
{
    NSString *str= [NSString stringWithFormat:@"http://api.guozhoumoapp.com/v1/posts/%@/comments?offset=0&limit=20",_model.ID];
    [HWRequestManager requestWithUrl:str parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *commentsArray = dataDic[@"comments"];
        self.dataArray = [NSMutableArray array];
        for (NSDictionary*oneDic in commentsArray) {
            HWCommentModel *cmodel = [[HWCommentModel alloc]init];
            [cmodel setValuesForKeysWithDictionary:oneDic];
            [_dataArray addObject:cmodel];
        }
        
        [_tableView reloadData];
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
    
}
- (void)addView
{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 110;
    [_tableView registerClass:[HWTopicsCommentTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    
    
    _avoidingView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _avoidingView.backgroundColor = [UIColor colorWithWhite:10 alpha:0.5];
    [self.view addSubview:_avoidingView];
    _targetView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight- 64, kScreenWidth, 64)];
    _targetView.backgroundColor = [UIColor lightGrayColor];
    [_avoidingView addSubview:_targetView];
    
    _textField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, _targetView.width-100, 30)];
    _textField1.placeholder = @"请输入.....";
    _textField1.borderStyle = UITextBorderStyleRoundedRect;
    [_targetView addSubview:_textField1];
    
    _but = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _but.frame = CGRectMake(_textField1.right, _textField1.top, _textField1.width/2, _textField1.height);
    [_but setTitle:@"发送" forState:(UIControlStateNormal)];
    [_but setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [_targetView addSubview:_but];
    [_but addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
   

}


- (void)button:(UIButton *)but
{
    HWCommentModel *model = [[HWCommentModel alloc]init];
    
    _message = _textField1.text;
    model.content = _message;
    ///model.nickname = @"1234567890";
    [_dataArray addObject:model];
    NSString*str = @"http://api.guozhoumoapp.com/v1/posts/147/comments";

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"content"] = [NSString stringWithFormat:@"%@",[_message stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    [HWRequestManager requestWithUrl:str parmDic:dic method:POST1 succeedWithData:^(NSData *data) {
        NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSLog(@"%@eeeeee",Dic);
        [_tableView reloadData];
    } failedWithError:^(NSError *connectionError) {
        
    }];
    _textField1.text = @"";
    [self.view endEditing:YES];
   
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    [self.view endEditing:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWTopicsCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    HWCommentModel *model = _dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
